refactor: add new filePath method to folder
authorJyrki Gadinger <nilsding@nilsding.org>
Thu, 24 Apr 2025 16:01:54 +0000 (18:01 +0200)
committerMatthieu Gallien <matthieu.gallien@nextcloud.com>
Fri, 30 May 2025 07:39:56 +0000 (09:39 +0200)
Signed-off-by: Jyrki Gadinger <nilsding@nilsding.org>
src/gui/folder.cpp
src/gui/folder.h
src/gui/tray/activitylistmodel.cpp

index 26df7db1272a7387d1b130960c7b2d20e8652d36..41772fa7042ed28ab65d613d0f26d055f48f5b52 100644 (file)
@@ -998,6 +998,26 @@ void Folder::migrateBlackListPath(const QString &legacyPath)
     }
 }
 
+QString Folder::filePath(const QString& fileName)
+{
+    const auto folderDir = QDir(_canonicalLocalPath);
+
+#ifdef Q_OS_WIN
+    // Edge case time!
+    // QDir::filePath checks whether the passed `fileName` is absolute (essentialy by using `!QFileInfo::isRelative()`).
+    // In the case it's absolute, the `fileName` will be returned instead of the complete file path.
+    //
+    // On Windows, if `fileName` starts with a letter followed by a colon (e.g. "A:BCDEF"), it is considered to be an
+    // absolute path.
+    // Since this method should return the file name file path starting with the canonicalLocalPath, catch that special case here and prefix it ourselves...
+    return fileName.length() >= 2 && fileName[1] == ':'
+           ? _canonicalLocalPath + fileName
+           : folderDir.filePath(fileName);
+#else
+    return folderDir.filePath(fileName);
+#endif
+}
+
 bool Folder::isFileExcludedAbsolute(const QString &fullPath) const
 {
     return _engine->excludedFiles().isExcluded(fullPath, path(), _definition.ignoreHiddenFiles);
index f66d4c81ba1a056970dd995396035676ed40f608..38d288d9df10ac6ee7e140d7ec9fa930403fa661 100644 (file)
@@ -165,6 +165,14 @@ public:
      */
     [[nodiscard]] QString remotePathTrailingSlash() const;
 
+    /**
+     * Returns the path name of a file in the local canonical path.
+     *
+     * Similar to `QDir(path()).filePath(...)`, except file names like "Z:test"
+     * are treated as relative paths on Windows.
+     */
+    [[nodiscard]] QString filePath(const QString& fileName);
+
     [[nodiscard]] QString fulllRemotePathToPathInSyncJournalDb(const QString &fullRemotePath) const;
 
     void setNavigationPaneClsid(const QUuid &clsid) { _definition.navigationPaneClsid = clsid; }
index 9bd40185972f6571cfdb22dcfcb93731b581547c..8e6df96921533ff0c4318500d0edd88a374b7726 100644 (file)
@@ -686,7 +686,6 @@ void ActivityListModel::slotTriggerDefaultAction(const int activityIndex)
         }
 
         auto folder = FolderMan::instance()->folder(activity._folder);
-        const auto folderDir = QDir(folder->path());
         const auto fileLocation = activity._syncFileItemStatus == SyncFileItem::FileNameInvalidOnServer
             ? InvalidFilenameDialog::FileLocation::NewLocalFile
             : InvalidFilenameDialog::FileLocation::Default;
@@ -694,21 +693,8 @@ void ActivityListModel::slotTriggerDefaultAction(const int activityIndex)
             ? InvalidFilenameDialog::InvalidMode::ServerInvalid
             : InvalidFilenameDialog::InvalidMode::SystemInvalid;
 
-#ifdef Q_OS_WIN
-        // Edge case time!
-        // QDir::filePath checks whether the passed `fileName` is absolute (essentialy by using `!QFileInfo::isRelative()`).
-        // On Windows, if `fileName` starts with a letter followed by a colon (e.g. "A:BCDEF"), it is considered to be an
-        // absolute path.
-        // Since the complete desired path is required by InvalidFilenameDialog, catch that special case here and prefix it ourselves...
-        const auto filePath = activity._file.length() >= 2 && activity._file[1] == ':'
-                              ? folder->path() + activity._file
-                              : folderDir.filePath(activity._file);
-#else
-        const auto filePath = folderDir.filePath(activity._file);
-#endif
-
         _currentInvalidFilenameDialog = new InvalidFilenameDialog(_accountState->account(), folder,
-            filePath, fileLocation, invalidMode);
+            folder->filePath(activity._file), fileLocation, invalidMode);
         connect(_currentInvalidFilenameDialog, &InvalidFilenameDialog::accepted, folder, [folder]() {
             folder->scheduleThisFolderSoon();
         });